//
//  PVDsListSearchPopView.swift
//  GreenDeal
//
//  Created by Moennig Q on 2023/8/23.
//

import UIKit
import SnapKit
import RxGesture
import RxSwift
import RxCocoa
enum PVDsListJobType: String{
    case pvdJob = "PV"
    case swhJob = "SWH"
    case all = "Job Type"
}
class PVDsListSearchPopView: View {
    
    fileprivate lazy var disposeBag = DisposeBag()
    //MARK: - 应用按钮
    let applyLabel = ContentLabel(R.font.interMedium(size: 17), hexStr: "#379657FF")
    //MARK: - 当前选择的值
    private var jobStatus: String = PVDJobStatusName.all.rawValue
    private var jobType: String = PVDsListJobType.all.rawValue
    //MARK: - 按钮左右两边的margin和
    let leftRightMargin: CGFloat = 20
    //MARK: - 按钮之间的间距
    let btnsInternal: CGFloat = 10
    //MARK: - 按钮的高度
    let btnHeight: CGFloat = 34
    
    //MARK: - job status
    let scheduledStatusLabel = JobStatusLabel(PVDJobStatusName.scheduled.rawValue)
    let installingStatusLabel = JobStatusLabel(PVDJobStatusName.installing.rawValue)
    let unqualifiedStatusLabel = JobStatusLabel(PVDJobStatusName.unqualified.rawValue)
    let installedStatusLabel = JobStatusLabel(PVDJobStatusName.installed.rawValue)
    //MARK: - job type
    var pvdTypeLabel: JobStatusLabel = {
        let view = JobStatusLabel(PVDsListJobType.pvdJob.rawValue)
        view.paddingLeft = 16
        view.paddingRight = 16
        return view
    }()
    var swhTypeLabel: JobStatusLabel = {
        let view = JobStatusLabel(PVDsListJobType.swhJob.rawValue)
        view.paddingLeft = 16
        view.paddingRight = 16
        return view
    }()
    var applyClosure: ((String,String)->Void)?
    //MARK: -
    override func setUpView() {
        self.backgroundColor = UIColor(hexStr: "#00000080")
        //
        let btnsPerRow: CGFloat = (UIDevice.getCurrentDeviceType() == APPConfig.iPadType ? 4 : 2)
        let btnsWidth: CGFloat = (APPConfig.WidthForPortrait - self.leftRightMargin * 2 - self.btnsInternal * (btnsPerRow - 1)) / btnsPerRow
        
        let backView = UIView()
        backView.backgroundColor = .white
        backView.drawCorner(byRoundingCorners: [.topLeft,.topRight],radii: 10)
        self.addSubview(backView)
        backView.snp.makeConstraints { make in
            make.bottom.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
            make.left.right.equalToSuperview()
        }
        //clear btn
        let clearAllLabel = ContentLabel(R.font.interMedium(size: 17), hexStr: "#292D32B2")
        clearAllLabel.text = R.string.localizable.clearAll()
        backView.addSubview(clearAllLabel)
        clearAllLabel.snp.makeConstraints { make in
            make.top.equalTo(10)
            make.left.equalTo(leftRightMargin)
            make.height.equalTo(40)
            make.width.greaterThanOrEqualTo(0)
        }
        //apply btn
        applyLabel.text = R.string.localizable.apply()
        backView.addSubview(applyLabel)
        applyLabel.snp.makeConstraints { make in
            make.centerY.height.equalTo(clearAllLabel)
            make.right.equalTo(-leftRightMargin)
            make.width.greaterThanOrEqualTo(0)
        }
        //line view
        let lineView = View(hexStr: Constant.SeparatorColorHexStr)
        backView.addSubview(lineView)
        lineView.snp.makeConstraints { make in
            make.top.equalTo(60)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        //job status title
        let jobStatusTitle = ContentLabel(R.font.interSemiBold(size: 17),hexStr: "#292D32FF")
        jobStatusTitle.text = R.string.localizable.jobStatus()
        backView.addSubview(jobStatusTitle)
        jobStatusTitle.snp.makeConstraints { make in
            make.top.equalTo(lineView.snp.bottom).offset(20)
            make.left.equalTo(leftRightMargin)
            make.size.greaterThanOrEqualTo(0)
        }
        //******jobStatusBtnsBackView
        let jobStatusBtnsBackView = UIView()
        jobStatusBtnsBackView.backgroundColor = .white
        backView.addSubview(jobStatusBtnsBackView)
        jobStatusBtnsBackView.snp.makeConstraints { make in
            make.top.equalTo(jobStatusTitle.snp.bottom).offset(16)
            make.left.equalTo(jobStatusTitle)
            make.right.equalTo(-leftRightMargin)
            make.height.greaterThanOrEqualTo(0)
        }
        //第一行,前两个都是有的
        jobStatusBtnsBackView.addSubview(scheduledStatusLabel)
        scheduledStatusLabel.snp.makeConstraints { make in
            make.left.top.equalToSuperview()
            make.height.equalTo(btnHeight)
            make.width.equalTo(btnsWidth)
        }
        jobStatusBtnsBackView.addSubview(installingStatusLabel)
        installingStatusLabel.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.left.equalTo(scheduledStatusLabel.snp.right).offset(btnsInternal)
            make.height.equalTo(btnHeight)
            make.width.equalTo(btnsWidth)
        }
        if btnsPerRow == 2{//iphone
            jobStatusBtnsBackView.addSubview(unqualifiedStatusLabel)
            unqualifiedStatusLabel.snp.makeConstraints { make in
                make.left.equalToSuperview()
                make.top.equalTo(scheduledStatusLabel.snp.bottom).offset(16)
                make.height.equalTo(btnHeight)
                make.width.equalTo(btnsWidth)
                make.bottom.equalToSuperview()
            }
            jobStatusBtnsBackView.addSubview(installedStatusLabel)
            installedStatusLabel.snp.makeConstraints { make in
                make.top.equalTo(unqualifiedStatusLabel)
                make.left.equalTo(unqualifiedStatusLabel.snp.right).offset(btnsInternal)
                make.height.equalTo(btnHeight)
                make.width.equalTo(btnsWidth)
            }
        }else{//其他显示4个，iPad
            jobStatusBtnsBackView.addSubview(unqualifiedStatusLabel)
            unqualifiedStatusLabel.snp.makeConstraints { make in
                make.top.equalToSuperview()
                make.left.equalTo(installingStatusLabel.snp.right).offset(btnsInternal)
                make.height.equalTo(btnHeight)
                make.width.equalTo(btnsWidth)
            }
            jobStatusBtnsBackView.addSubview(installedStatusLabel)
            installedStatusLabel.snp.makeConstraints { make in
                make.top.equalToSuperview()
                make.left.equalTo(unqualifiedStatusLabel.snp.right).offset(btnsInternal)
                make.height.equalTo(btnHeight)
                make.width.equalTo(btnsWidth)
                make.bottom.equalToSuperview()
            }
        }
        //**********job type title
        let jobTypeTitle = ContentLabel(R.font.interSemiBold(size: 17),hexStr: "#292D32FF")
        jobTypeTitle.text = JobType.all.rawValue
        backView.addSubview(jobTypeTitle)
        jobTypeTitle.snp.makeConstraints { make in
            make.top.equalTo(jobStatusBtnsBackView.snp.bottom).offset(30)
            make.left.equalTo(jobStatusTitle)
            make.size.greaterThanOrEqualTo(0)
        }
        //open
        backView.addSubview(pvdTypeLabel)
        pvdTypeLabel.snp.makeConstraints { make in
            make.top.equalTo(jobTypeTitle.snp.bottom).offset(16)
            make.left.equalTo(jobTypeTitle)
            make.height.equalTo(btnHeight)
            make.width.equalTo(btnsWidth)
            make.bottom.equalTo(-65)
        }
        //open
        backView.addSubview(swhTypeLabel)
        swhTypeLabel.snp.makeConstraints { make in
            make.centerY.equalTo(pvdTypeLabel)
            make.left.equalTo(pvdTypeLabel.snp.right).offset(btnsInternal)
            make.height.equalTo(btnHeight)
            make.width.equalTo(btnsWidth)
        }
        
        //上方空白区域，点击时关闭view
        let blankView = UIView()
        blankView.backgroundColor = .clear
        self.addSubview(blankView)
        blankView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
            make.bottom.equalTo(backView.snp.top)
        }
        //点击空白区域关闭此pop view
        blankView.rx.tapGesture().when(.recognized).subscribe(onNext: { [weak self] _ in
            self?.removeFromSuperview()
        }).disposed(by: self.disposeBag)
        //MARK: - 两个按钮的点击
        //clear all绑定
        clearAllLabel.rx.tapGesture().when(.recognized).subscribe(onNext: { [weak self] _ in
            self?.clearAll()
        }).disposed(by: self.disposeBag)
        //apply绑定
        applyLabel.rx.tapGesture().when(.recognized).subscribe(onNext: { [weak self] _ in
            guard let strongSelf = self else { return  }
            strongSelf.applyClosure?(strongSelf.jobStatus, strongSelf.jobType)
            strongSelf.removeFromSuperview()
        }).disposed(by: self.disposeBag)
        //MARK: - job status五个按钮的点击
        //scheduled label绑定
        scheduledStatusLabel.rx.tapGesture().when(.recognized).subscribe(onNext: { [weak self] _ in
            guard let strongSelf = self else { return  }
            strongSelf.selJobStatus(PVDJobStatusName.scheduled.rawValue)
        }).disposed(by: self.disposeBag)
        //unqualified label绑定
        unqualifiedStatusLabel.rx.tapGesture().when(.recognized).subscribe(onNext: { [weak self] _ in
            guard let strongSelf = self else { return  }
            strongSelf.selJobStatus(PVDJobStatusName.unqualified.rawValue)
        }).disposed(by: self.disposeBag)
        //installing绑定
        installingStatusLabel.rx.tapGesture().when(.recognized).subscribe(onNext: { [weak self] _ in
            guard let strongSelf = self else { return  }
            strongSelf.selJobStatus(PVDJobStatusName.installing.rawValue)
        }).disposed(by: self.disposeBag)
        //installed label绑定
        installedStatusLabel.rx.tapGesture().when(.recognized).subscribe(onNext: { [weak self] _ in
            guard let strongSelf = self else { return  }
            strongSelf.selJobStatus(PVDJobStatusName.installed.rawValue)
        }).disposed(by: self.disposeBag)
        
        //MARK: - job type五个按钮的点击
        //pvd type label绑定
        pvdTypeLabel.rx.tapGesture().when(.recognized).subscribe(onNext: { [weak self] _ in
            guard let strongSelf = self else { return  }
            strongSelf.selJobType(PVDsListJobType.pvdJob.rawValue)
        }).disposed(by: self.disposeBag)
        //swh type label绑定
        swhTypeLabel.rx.tapGesture().when(.recognized).subscribe(onNext: { [weak self] _ in
            guard let strongSelf = self else { return  }
            strongSelf.selJobType(PVDsListJobType.swhJob.rawValue)
        }).disposed(by: self.disposeBag)
    }
    func initData(jobStatus: String, jobType: String){
        self.jobStatus = jobStatus
        self.jobType = jobType
        self.initJobStatus(jobStatus)
        self.initJobType(jobType)
        
    }
    private func initJobStatus(_ jobStatus: String){
        self.unSelAllJobStatus()
        if jobStatus.contains(PVDJobStatusName.scheduled.rawValue + ","){
            scheduledStatusLabel.setSelect(onFlag: true)
        }
        if jobStatus.contains(PVDJobStatusName.unqualified.rawValue + ","){
            unqualifiedStatusLabel.setSelect(onFlag: true)
        }
        if jobStatus.contains(PVDJobStatusName.installing.rawValue + ","){
            installingStatusLabel.setSelect(onFlag: true)
        }
        if jobStatus.contains(PVDJobStatusName.installed.rawValue + ","){
            installedStatusLabel.setSelect(onFlag: true)
        }
    }
    private func initJobType(_ jobType: String){
        self.unSelAllJobType()
        if jobType.contains(PVDsListJobType.pvdJob.rawValue + ","){
            pvdTypeLabel.setSelect(onFlag: true)
        }
        if jobType.contains(PVDsListJobType.swhJob.rawValue + ","){
            swhTypeLabel.setSelect(onFlag: true)
        }
    }
    //
    private func selectJobStatusByValue(status: String, onFlag: Bool){
        switch status {
        case PVDJobStatusName.installing.rawValue:
            installingStatusLabel.setSelect(onFlag: onFlag)
        case PVDJobStatusName.unqualified.rawValue:
            unqualifiedStatusLabel.setSelect(onFlag: onFlag)
        case PVDJobStatusName.scheduled.rawValue:
            scheduledStatusLabel.setSelect(onFlag: onFlag)
        case PVDJobStatusName.installed.rawValue:
            installedStatusLabel.setSelect(onFlag: onFlag)
        default:
            break
        }
    }
    //
    private func selectJobTypeByValue(status: String, onFlag: Bool){
        switch status {
        case PVDsListJobType.pvdJob.rawValue:
            pvdTypeLabel.setSelect(onFlag: onFlag)
        case PVDsListJobType.swhJob.rawValue:
            swhTypeLabel.setSelect(onFlag: onFlag)
        default:
            break
        }
    }
    private func selJobStatus(_ status: String){
        if self.jobStatus == PVDJobStatusName.all.rawValue {
            self.jobStatus = status + ","
            self.selectJobStatusByValue(status: status, onFlag: true)
        }else{
            if self.jobStatus.contains(status + ","){//取消选择
                self.jobStatus = self.jobStatus.replacingOccurrences(of: status + ",", with: "")
                if self.jobStatus == "" {
                    self.jobStatus = PVDJobStatusName.all.rawValue
                }
                
                self.selectJobStatusByValue(status: status, onFlag: false)
            }else{//选中
                self.jobStatus += status + ","
                self.selectJobStatusByValue(status: status, onFlag: true)
            }
        }
    }
    private func selJobType(_ status: String){
        if self.jobType == PVDsListJobType.all.rawValue {//首次选择
            self.jobType = status + ","
            self.selectJobTypeByValue(status: status, onFlag: true)
        }else{
            if self.jobType.contains(status + ","){//取消选择
                self.jobType = self.jobType.replacingOccurrences(of: status + ",", with: "")
                if self.jobType == "" {
                    self.jobType = PVDsListJobType.all.rawValue
                }
                self.selectJobTypeByValue(status: status, onFlag: false)
            }else{
                self.jobType += status + ","
                self.selectJobTypeByValue(status: status, onFlag: true)
            }
            
        }
    }
    private func clearAll(){
        //清空选择的值
        jobStatus = PVDJobStatusName.all.rawValue
        jobType = JobType.all.rawValue
        //
        unSelAllJobStatus()
        unSelAllJobType()
    }
    //MARK: - 取消全部选择
    private func unSelAllJobStatus(){
        installingStatusLabel.setSelect(onFlag: false)
        scheduledStatusLabel.setSelect(onFlag: false)
        unqualifiedStatusLabel.setSelect(onFlag: false)
        installedStatusLabel.setSelect(onFlag: false)
    }
    
    //MARK: - 取消payment status全部选择
    private func unSelAllJobType(){
        pvdTypeLabel.setSelect(onFlag: false)
        swhTypeLabel.setSelect(onFlag: false)
    }
}
